{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3fef001a",
   "metadata": {},
   "source": [
    "# 上周知识点\n",
    "* 1.Json(文本数据) --->Dict(python中的字典数据)\n",
    "* 2.字典和列表的实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "46e18e8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：了解数据中有哪些唯一存在的值？（假如是列表）\n",
    "no_duplicate_age = []\n",
    "list_age = [18, 24, 25, 24, 18, 23, 18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3fb0d907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 25, 23]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac5f58b6",
   "metadata": {},
   "source": [
    "## 本周知识点:集合\n",
    "\n",
    "1.集合的性质 \n",
    "* 无序(unordered)\n",
    "* 不重复(no duplicate)\n",
    "\n",
    "2.集合的一般方法   \n",
    "* 增加\n",
    "* 删除\n",
    "* 查询：in 查询成员关系\n",
    "\n",
    "3.集合的数学逻辑运算\n",
    "* 交集\n",
    "* 并集\n",
    "* 差集\n",
    "* 对称差集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25c7069c",
   "metadata": {},
   "source": [
    "## 1.集合的性质 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf3e3469",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 25, 18, 23}\n"
     ]
    }
   ],
   "source": [
    "# list --->set 的方法：set()\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8c81d06d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'orange', 'banana', 'pear'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.移除重复项\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)\n",
    "# 2.in 查询成员关系\n",
    "'orange' in basket         \n",
    "'crabgrass' in basket\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33b7d1f2",
   "metadata": {},
   "source": [
    "## 2.集合的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b2ef3c",
   "metadata": {},
   "source": [
    "* 1.集合的创建两种一般方法：\n",
    "> 1.set(list)  \n",
    "> 2.直接按照集合的结构创建：{value1，value2，value3，...}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72f27e69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b5f0b85",
   "metadata": {},
   "source": [
    "* 2.增加 add()\n",
    "> 1.结果：增加元素到集合中\n",
    "> 2.注意：增加已经存在的元素不会影响结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d50a39f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d0e8ce8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40b8dcb7",
   "metadata": {},
   "source": [
    "* 3.删除pop()\n",
    "> 1. pop()\n",
    "> 2. remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "74e5117c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n",
      "{1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3acac69f",
   "metadata": {},
   "outputs": [],
   "source": [
    "set_2 = set([])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85711265",
   "metadata": {},
   "source": [
    "* 4.删除remove\n",
    "> 1.一般项目中remove()会和in共同使用，避免报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "79478184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 99, 7, 8}\n",
      "{1, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c12cd8e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 避免keyerror的方法\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bed94fb",
   "metadata": {},
   "source": [
    "* 5.删除discard\n",
    "> 1.删除的元素不在集合中，不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "04c18789",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 99, 7, 8}\n",
      "{1, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c85671b8",
   "metadata": {},
   "source": [
    "## 3.集合的数学逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b8018583",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2501e14b",
   "metadata": {},
   "source": [
    "* 1.集合的交集运算\n",
    "> 1.使用集合的方法：set_1.intersection()   \n",
    "> 2.使用逻辑运算符：set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "83ac400b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用集合的方法：set_1.intersection(set_2)\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"交集结果：\",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "255ed218",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 2.使用集合的方法：set_1 & set_2\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"交集结果：\",set_1 & set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9679b8f",
   "metadata": {},
   "source": [
    "* 2.集合的并集运算\n",
    "> 1.使用并集的方法：set_1.union(set_2)   \n",
    "> 2.使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fa3fcda4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "并集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用并集的方法：set_1.union(set_2)\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"并集结果：\",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "070cba26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "并集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2.使用逻辑运算符：set_1 | set_2\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"并集结果：\",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2ccfeea7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间： 1637909474.16923\n",
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "并集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间： 1637909474.170229\n",
      "运行的时间： 0.0009989738464355469\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 (运行后的时间节点-运行前的时间结点)\n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间：\",start_time)\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"并集结果：\",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间：\",stop_time)\n",
    "print(\"运行的时间：\",stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c8abfaf",
   "metadata": {},
   "source": [
    "* 3.集合的差集运算（考虑先后顺序）\n",
    "> 1.使用差集的方法：set_1.difference(set_2)   \n",
    "> 2.使用逻辑运算符：set_1 - set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "904b26df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1差集结果： {2, 5, 6}\n",
      "set_2差集结果： {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用差集的方法：set_1.difference(set_2) \n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"set_1差集结果：\",set_1.difference(set_2))\n",
    "print(\"set_2差集结果：\",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "943a01e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1差集结果： {2, 5, 6}\n",
      "set_2差集结果： {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "# 2.使用逻辑运算符：set_1 - set_2\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"set_1差集结果：\",set_1 - set_2)\n",
    "print(\"set_2差集结果：\",set_2 - set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78c39cf6",
   "metadata": {},
   "source": [
    "* 5.对称差集\n",
    "> 1.用对称差集的方法：set_1.symmetric_difference(set_2)   \n",
    "> 2.使用逻辑运算符：？(set_1 - set_2) | (set_2 - set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "732cd29b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果： {0, 66, 2, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.用对称差集的方法：set_1.symmetric_difference(set_2)   \n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"对称差集结果：\",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f6802f80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 : {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 : {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果： {0, 2, 66, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2.使用逻辑运算符：？(set_1 - set_2) | (set_2 - set_1)\n",
    "print(\"set_1 :\",set_1)\n",
    "print(\"set_2 :\",set_2)\n",
    "print(\"对称差集结果：\",(set_1 - set_2) | (set_2 - set_1))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c7acee13",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cba160e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels :  {'i', 'e', 'u', 'o', 'a'}\n",
      "Provide a word to search for vowels:hello\n",
      "word :  {'e', 'l', 'h', 'o'}\n",
      "{'e', 'o'}\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels : \",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word : \",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "242deb8d",
   "metadata": {},
   "source": [
    "## 总结集合方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c1c42d0",
   "metadata": {},
   "source": [
    "| names.| 用法 | 说明 |\n",
    "| :--- | :---: | :---: |\n",
    "| add() | set1.add() | 向 set1 集合中添加数字、字符串、元组或者布尔类型 |\n",
    "| clear() | set1.clear() | 清空 set1 集合中所有元素 |\n",
    "| copy() | set2 = set1.copy() | copy set1 集合给 set2|\n",
    "| difference() | set3 = set1.difference(set2) | 将 set1 中有而 set2 没有的元素给 set3 |\n",
    "| discard() | set1.discard(elem) | 删除 set1 中的 elem 元素 |\n",
    "| intersection() | set3 = set1.intersection(set2) | 取 set1 和 set2 的交集给 set3 |\n",
    "| pop() | set1.pop() | 删除set1 中一个元素 |\n",
    "| remove() | set1.remove(elem) | 移除 set1 中的 elem 元素 |\n",
    "| symmetric_difference() | set3 = set1.symmetric_difference(set2) | 取 set1 和 set2 中互不相同的元素，给 set3 |\n",
    "| union() | set3 = set1.union(set2) | 取 set1 和 set2 的并集，赋给 set3 |\n",
    "| update() | set1.update(elem) | 添加列表或集合中的元素到 set1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
